﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JoystickInput : IUserInput {
    [Header("===== joystick settings =====")]
    public string axisX = "axisX";
    public string axisY = "axisY";
    public string axisJright = "axis4";
    public string axisJup= "axis5";
    public string btnA = "btn0";
    public string btnB = "btn1";
    public string btnX = "btn2";
    public string btnY = "btn3";
    public string btnLB = "btn4";
    public string btnLT = "btn6";
    //[Header("===== Output signal =====")]
    //public float Dup;
    //public float Dright;
    //public float Dmag;
    //public Vector3 Dvec;

    //public float Jup;
    //public float Jright;

    ////1. pressing signal
    //public bool run;
    ////2. trigger once signal
    //public bool jump;
    //private bool lastJump;
    //public bool attack;
    //private bool lastAttack;
    ////3. double trigger
    //[Header("===== Others =====")]
    //public bool inputEnable = true;

    //private float targetDup;
    //private float targetDright;
    //private float velocityDup;
    //private float velocityDright;


    // Update is called once per frame
    void Update () {

        Jup = -1 * Input.GetAxis(axisJup);
        Jright = Input.GetAxis(axisJright);

        //自定义纵横轴输入信号
        targetDup = Input.GetAxis(axisY);
        targetDright = Input.GetAxis(axisX);

        if (!inputEnable)
        {
            targetDup = 0;
            targetDright = 0;

        }
        Dup = Mathf.SmoothDamp(Dup, targetDup, ref velocityDup, 0.1f);
        Dright = Mathf.SmoothDamp(Dright, targetDright, ref velocityDright, 0.1f);

        Vector2 tmepDAxis = SquareToCircle(new Vector2(Dright, Dup));
        Dmag = new Vector2(tmepDAxis.y, tmepDAxis.x).magnitude;
        Dvec = Dright * transform.right + Dup * transform.forward;

        run = Input.GetButton(btnA);
        defense = Input.GetButton(btnLB);


        bool newJump = Input.GetButton(btnB);
        jump = (newJump != lastJump && newJump == true) ? true : false;
        lastJump = newJump;

        bool newAttack = Input.GetButton(btnX);
        attack = (newAttack != lastAttack && newAttack == true) ? true : false;
        lastAttack = newAttack;
    }

    //private Vector2 SquareToCircle(Vector2 input)
    //{
    //    Vector2 output = Vector2.zero;

    //    output.x = input.x * Mathf.Sqrt(1 - (input.y * input.y) / 2.0f);
    //    output.y = input.y * Mathf.Sqrt(1 - (input.x * input.x) / 2.0f);
    //    return output;
    //}
}
